#include <bits/stdc++.h>
using  namespace std;
const int N = 110;
int n;

struct Range {
	string s;
	int y, m, d;
	bool operator< (const Range &W)const {
		if (y < W.y) {
			return true;
		} else if (y > W.y) {
			return false;
		} else {
			if (m < W.m) {
				return true;
			} else if (m > W.m) {
				return false;
			} else {
				return d <= W.d;
			}
		}
	}
} range[N];

int main() {
	cin >> n;
	for (int i = 0; i < n; ++i) {
		string a;
		int b, c, d;
		cin >> a >> b >> c >> d;
		range[i] = {a, b, c, d};
	}
	stable_sort(range, range + n);
	for (int i = 0; i < n; ++i) {
		cout << range[i].s << endl;
	}
	return 0;
}